<template>
  <el-dialog :ref="dialogRef" v-el-drag-dialog :visible.sync="visible" :show-close="false" :is-footer="isFooter" :title="title" :width="!isminimize ? width : '25%'" :class="isminimize ? 'isminimize' :''" :fullscreen="isfullscreen" :append-to-body="appendToBody" @dragDialog="handleDrag" @close="handleClose">

    <!-- Dialog Header -->
    <div slot="title">
      <el-row>
        <el-col :span="17" class="dialog-title">{{ title }}</el-col>
        <el-col :span="7" class="dialog-action" style="text-align:right;cursor: pointer;">
          <svg-icon class="action-btn isminimize" icon-class="zuixiaohua" @click="minimize" />
          <svg-icon class="action-btn isfullscreen" icon-class="zuidahua" @click="fullscreen" />
          <svg-icon class="action-btn close" icon-class="guanbi" @click="handleClose" />
        </el-col>
      </el-row>
    </div>

    <!-- Dialog Body & Footer -->
    <div v-show="!isminimize">
      <slot name="body" />
    </div>

    <slot v-if="!isminimize" name="footer" />
  </el-dialog>
</template>

<script>
// 拖拽自定义指令
import elDragDialog from '@/directive/el-drag-dialog'

export default {
  name: 'ZrkDragDialog',
  directives: { elDragDialog },
  props: {
    show: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    width: {
      type: [String, Number],
      default: '50%'
    },
    isFooter: {
      type: Boolean,
      default: false
    },
    appendToBody: {
      type: Boolean,
      default: true
    },
    dialogRef: {
      type: String,
      default: 'ZrkDragDialog'
    }
  },
  data() {
    return {
      visible: this.show, // 显示|隐藏 Dialog
      isfullscreen: false, // 全屏
      isminimize: false // 最小化
    }
  },
  // 关闭后全部重置
  watch: {
    visible(val) {
      if (val) {
        this.resetPosition()
        this.isminimize = false
        this.isfullscreen = false
      }
    },
    show: {
      immediate: true,
      handler(val) {
        this.visible = this.show
      }
    }
  },
  methods: {
    // 拖拽可触发的回调函数
    handleDrag() {},
    // 最小化
    minimize() {
      this.isminimize = !this.isminimize
      if (this.isfullscreen) this.isfullscreen = !this.fullscreen
    },
    // 最大化
    fullscreen() {
      this.isfullscreen = !this.isfullscreen
      this.resetPosition()
      if (this.isminimize) this.isminimize = !this.isminimize
    },
    // 关闭
    handleClose() {
      this.$emit('update:show', false)
    },
    // 重置定位
    resetPosition() {
      const el = this.$refs[this.dialogRef].$el.querySelector('.el-dialog')
      el.style.left = 0
      el.style.top = 0
    }
  }
}
</script>

<style lang="scss" scoped>
.dialog-title {
  font-size: 18px;
  color: #303133;
}

.isminimize {
  $padding: 0;
  ::v-deep .el-dialog__body {
    padding: $padding !important;
  }
  ::v-deep .el-dialog__footer {
    padding: $padding !important;
  }
}

::v-deep .el-dialog__header {
  padding: 16px 24px;
  border-bottom: 1px solid #e8e8e8;
  color: rgba(0, 0, 0, 0.65);
}

::v-deep .el-dialog__body {
  padding: 24px !important;
}

.isfullscreen {
  vertical-align: -2.8px;
}

.isminimize {
  font-size: 16px !important;
  vertical-align: -3.2px;
}

.close {
  font-size: 12px !important;
  vertical-align: -1.6px;
}

.action-btn {
  font-size: 14px;
  margin: 4px 0 0 18px;

  &:hover {
    color: #409eff;
  }
}
</style>
